/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.sql.SQLOutput;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javase - T20
 * <p>Powered by webrx On 2021-12-27 13:35:36
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T20 {
    void work() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s:线程启动%n", tn);
        for (int i = 0; i < 2; i++) {
            System.out.printf("%s:%d%n", tn, i);
        }
        System.out.printf("%s:线程结束%n", tn);
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService es = Executors.newCachedThreadPool();
        //建立固定大小的线程池
        //es = Executors.newFixedThreadPool(3);
        es = Executors.newSingleThreadExecutor();

        var t = new T20();
        //es.submit(t::work, "T1");
        //es.submit(t::work, "T2");
        //es.submit(t::work, "T3");
        //es.submit(t::work, "T4");

        es.execute(t::work);
        es.execute(t::work);
        es.execute(t::work);
        es.execute(t::work);

        System.out.println(es.isShutdown());

        // 关闭启动线程
        es.shutdown();
        // 等待子线程结束，再继续执行下面的代码
        es.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
        System.out.println("all thread complete");

    }
}
